package com.hw.one.collect.util;

import org.apache.commons.codec.binary.Base64;

import java.io.UnsupportedEncodingException;

public class Base64Util {

    public static String base64(String str) throws UnsupportedEncodingException {
        return new String(encodeBase64(str.getBytes("UTF-8")));
    }

    public static String base64(String str, String charset) throws UnsupportedEncodingException {
        return new String(encodeBase64(str.getBytes(charset)));
    }

    public static String base64(byte[] buf) {
        if (buf == null) {
            return "";
        }
        return new String(Base64.encodeBase64(buf));
    }

    public static byte[] encodeBase64(byte[] buf) {
        if (buf == null) {
            return null;
        }
        return Base64.encodeBase64(buf);
    }

    public static byte[] decodeBase64(String str) {
        if (str == null) {
            return null;
        }
        return Base64.decodeBase64(str.getBytes());
    }

    public static byte[] decodeBase64(byte[] buf) {
        if (buf == null) {
            return null;
        }
        return Base64.decodeBase64(buf);
    }

}
